package com.vshop.platform.system.api.impl;

import com.vshop.auth.api.ApiUserService;
import com.vshop.auth.api.domain.dto.ApiLoginUser;
import com.vshop.auth.api.domain.dto.ApiRole;
import com.vshop.framework.core.util.MapstructUtil;
import com.vshop.platform.system.crud.dao.SysDeptDao;
import com.vshop.platform.system.crud.dao.SysRoleDao;
import com.vshop.platform.system.crud.dao.SysUserDao;
import com.vshop.platform.system.crud.service.PermissionService;
import com.vshop.platform.system.domain.entity.SysDept;
import com.vshop.platform.system.domain.entity.SysRole;
import com.vshop.platform.system.domain.entity.SysUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@RequiredArgsConstructor
@Service
public class ApiUserServiceImpl implements ApiUserService {

    private final PermissionService permissionService;
    private final SysUserDao sysUserDao;
    private final SysDeptDao sysDeptDao;
    private final SysRoleDao sysRoleDao;

    @Override
    public ApiLoginUser getUserInfo(String username) {
        SysUser user = sysUserDao.getUserInfo(username);
        return buildLoginUser(user);
    }

    @Override
    public ApiLoginUser getUserInfo(Long userId, String tenantId) {
        return null;
    }

    @Override
    public ApiLoginUser getUserInfoByPhonenumber(String phoneNumber, String tenantId) {
        return null;
    }

    @Override
    public ApiLoginUser getUserInfoByEmail(String email, String tenantId) {
        return null;
    }

    @Override
    public String selectUserNameById(Long userId) {
        return "";
    }

    @Override
    public String selectNicknameById(Long userId) {
        return "";
    }

//    @Override
//    public void updateLoginInfo(Long userId, String ip) {
//        SysUser sysUser = new SysUser();
//        sysUser.setUserId(userId);
//        sysUser.setLoginIp(ip);
//        sysUser.setLoginDate(LocalDateTime.now());
//        userRepository.updateById(sysUser);
//    }

    /**
     * 构建登录用户
     */
    private ApiLoginUser buildLoginUser(SysUser sysUser) {
        SysDept sysDept = sysDeptDao.getById(sysUser.getDeptId());
        List<SysRole> sysRoles = sysRoleDao.listUserRoleByUserId(sysUser.getUserId());

        ApiLoginUser loginUser = new ApiLoginUser();
        loginUser.setTenantId(sysUser.getTenantId());
        loginUser.setUserId(sysUser.getUserId());
        loginUser.setDeptId(sysUser.getDeptId());
        loginUser.setUsername(sysUser.getUserName());
        loginUser.setNickname(sysUser.getNickName());
        loginUser.setPassword(sysUser.getPassword());
        loginUser.setUserType(sysUser.getUserType());
        loginUser.setMenuPermission(permissionService.getMenuPermission(sysUser.getUserId()));
        loginUser.setRolePermission(permissionService.getRolePermission(sysUser.getUserId()));
        loginUser.setDeptName(sysDept.getDeptName());
        List<ApiRole> roles = MapstructUtil.convert(sysRoles, ApiRole.class);
        loginUser.setRoles(roles);
        return loginUser;
    }
}
